define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cut/index' + location.search,
                    add_url: 'cut/add',
                    edit_url: 'cut/edit',
                    del_url: 'cut/del',
                    multi_url: 'cut/multi',
                    import_url: 'cut/import',
                    table: 'cut',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit,.btn-addone,.btn-add").data("area", ["70%", "85%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'factory_id', title: __('Factory_id'), sortable: true, formatter: Table.api.formatter.status, searchList: Config.Factorys},
                        {field: 'status', title: __('Status'), sortable: true, formatter: Table.api.formatter.status, searchList: __('Order_status')},
                        {field: 'sn', title: __('Sn')},
                        {field: 'cost', title: __('Cost'), operate:'BETWEEN'},
                        {field: 'process_fee', title: __('Process_fee'), operate:'BETWEEN'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'clothNo', title: __('clothNo')},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD HH:mm"},
                        {field: 'editer_id', title: __('Editer_id'), formatter: Table.api.formatter.status, searchList: Config.Admins},
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
								{
                                    name: 'cut',
                                    text: '裁床',
                                    title: '裁床',
                                    classname: 'btn btn-xs btn-primary  btn-dialog',
                                    extend: 'data-area=\'["99%","99%"]\' data-shade=\'[0.5,"#000"]\'',
                                    url: function ($row) {
   									    return 'cut/cut?ids='+ $row.id;
   									},
                                    refresh: true
                                },
                                {
                                    name: 'cuted',
                                    text: '裁床',
                                    title: '裁床',
                                    classname: 'btn btn-xs btn-default  btn-dialog',
                                    extend: 'data-area=\'["99%","99%"]\' data-shade=\'[0.5,"#000"]\'',
                                    url: function ($row) {
   									    return 'cut/cut?ids='+ $row.id;
   									},
                                    refresh: true
                                }
                            ],
                            formatter: function (value, row, index) {
                        	    var that = $.extend({}, this);
                        	    var table = $(that.table).clone(true);
                        	    if(row.is_cut == 1){
                        	    	$(table).data("operate-cut", null);
                        	    } else {
                        	    	$(table).data("operate-cuted", null);
                        	    }
                        	    
                        	    that.table = table;                        	    
                        	    return Table.api.formatter.operate.call(that, value, row, index);
                        	}
                        }
                     ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        produce: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cut/index' + location.search,
                    add_url: 'cut/add',
                    edit_url: 'cut/edit',
                    del_url: 'cut/del',
                    multi_url: 'cut/multi',
                    import_url: 'cut/import',
                    table: 'cut',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('行号')},
                        {field: 'factory_id', title: __('Factory_id'), sortable: true, formatter: Table.api.formatter.status, searchList: Config.Factorys},
                        {field: 'clothtype_id', title: __('Clothtype_id')},
                        {field: 'status', title: __('Status'), sortable: true, formatter: Table.api.formatter.status, searchList: __('Order_status')},
                        {field: 'sn', title: __('Sn')},
                        {field: 'cost', title: __('Cost'), operate:'BETWEEN'},
                        {field: 'process_fee', title: __('Process_fee'), operate:'BETWEEN'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD HH:mm"},
                        {field: 'editer_id', title: __('Editer_id'), formatter: Table.api.formatter.status, searchList: Config.Admins},

                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
								{
                                    name: 'admins',
                                    text: '裁床',
                                    title: '裁床',
                                    classname: 'btn btn-xs btn-primary  btn-dialog',
                                    extend: 'data-area=\'["99%","99%"]\' data-shade=\'[0.5,"#000"]\'',
                                    url: function ($row) {
   									    return 'cut/cut?ids='+ $row.id;
   									},
                                    refresh: true
                                }							
                            ],
                            formatter: Table.api.formatter.operate
                        }
                     ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'cut/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'cut/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'cut/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        cut: function () {
        	$(document).on("fa.event.appendfieldlist", "#bill .btn-append", function (e, obj) {
                //绑定动态下拉组件
                Form.events.selectpage(obj);
                //绑定上传组件
                Form.events.faupload(obj);
                //上传成功回调事件，变更按钮的背景 
                $(".upload-image", obj).data("upload-success", function (data) {
                    $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                })
            });
            Controller.api.bindevent();
            //$(".btn-danger").hide();
            //点击打开对应的采购单详情
            $(document).on("click", ".sn", function (e) {
            	var purchase_id = $(this).data('id');
            	var purchase_sn = $(this).data('sn');
            	Backend.api.open('purchase/edit/ids/' + purchase_id, __('采购单:') + purchase_sn, {area:['95%', '99%']});           	
            });
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
